﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TT.ORM;
using System.Collections;

namespace Bill.BasForm
{
    public partial class frmSelCitys : Form
    {
        //public frmSelCitys()
        //{
        //    InitializeComponent();
        //}
        string type = "";//类型 S:省 //C:市 //Y:院
        IList<etyBaseTable> sourceList;
        public string SelNo = "";
        public string SelName = "";
        /// <summary>
        /// 
        /// </summary>
        /// <param name="type">类型 S:省 //C:市 //Y:院</param>
        /// <param name="srcList"></param>
        public frmSelCitys(string type, IList<etyBaseTable> srcList)
        {
            InitializeComponent();
            this.type = type;
            this.sourceList = new List<etyBaseTable>();
            foreach (etyBaseTable src in srcList)
            {
                if (src.X_CODE != "***")
                    sourceList.Add(src);
            }
        }
        private void frmSelCitys_Load(object sender, EventArgs e)
        {
            IList<etyBaseTable> srcList = sourceList;
            IList<etyBaseTable> dstList = new List<etyBaseTable>();
            if (!string.IsNullOrWhiteSpace(SelNo))
            {
                string[] arrSelNo = SelNo.Split(',');
                foreach (string selNo in arrSelNo)
                {
                    if (!string.IsNullOrWhiteSpace(selNo))
                    {
                        for (int i = srcList.Count - 1; i >= 0; i--)
                        {
                            etyBaseTable src = srcList[i];
                            if (src.X_CODE == selNo)
                            {
                                dstList.Add(src);
                                srcList.Remove(src);
                            }
                        }
                    }
                }
            }
            string sMapingCols = "X_CODE,X_CNAME";
            string sMapingHeaderTxt = "";
            switch (type)
            {
                case "S":
                    sMapingHeaderTxt = "编号,省份";
                    break;
                case "C":
                    sMapingHeaderTxt = "编号,市";
                    break;
                case "Y":
                    sMapingHeaderTxt = "编号,院";
                    break;
                default:
                    break;
            }

            gdSrc.GridView = this.gridView1;
            gdSrc.ColumnsMappingCreate(sMapingCols, sMapingHeaderTxt);
            bindingSource1.DataSource = srcList;
            gdSrc.DataSource = bindingSource1;

            gdDst.GridView = this.gridView2;
            gdDst.ColumnsMappingCreate(sMapingCols, sMapingHeaderTxt);
            bindingSource2.DataSource = dstList;
            gdDst.DataSource = bindingSource2;
        }

        private void btRight_Click(object sender, EventArgs e)
        {
            SrcToDst();


        }
        private void SrcToDst()
        {
            etyBaseTable selEty = bindingSource1.Current as etyBaseTable;
            if (selEty == null)
                return;
            IList<etyBaseTable> srcList = bindingSource1.DataSource as IList<etyBaseTable>;
            IList<etyBaseTable> dstList = bindingSource2.DataSource as IList<etyBaseTable>;

            srcList.Remove(selEty);
            dstList.Add(selEty);

            bindingSource1.DataSource = srcList;
            gdSrc.DataSource = bindingSource1;
            gdSrc.RefreshDataSource();
            bindingSource2.DataSource = dstList;
            gdDst.DataSource = bindingSource2;
            gdDst.RefreshDataSource();
        }


        private void btLeft_Click(object sender, EventArgs e)
        {
            DstToSrc();

        }
        private void DstToSrc()
        {
            etyBaseTable selEty = bindingSource2.Current as etyBaseTable;
            IList<etyBaseTable> srcList = bindingSource1.DataSource as IList<etyBaseTable>;
            IList<etyBaseTable> dstList = bindingSource2.DataSource as IList<etyBaseTable>;

            dstList.Remove(selEty);
            srcList.Add(selEty);

            bindingSource1.DataSource = srcList;
            gdSrc.DataSource = bindingSource1;
            gdSrc.RefreshDataSource();
            bindingSource2.DataSource = dstList;
            gdDst.DataSource = bindingSource2;
            gdDst.RefreshDataSource();
        }

        private void tsbCommit_Click(object sender, EventArgs e)
        {
            IList<etyBaseTable> dstList = bindingSource2.DataSource as IList<etyBaseTable>;
            if (dstList.Count < 2)
            {
                MessageBox.Show("请选择至少两条记录！");
                return;
            }
            foreach (etyBaseTable ety in dstList)
            {
                SelNo += ety.X_CODE + ",";
                SelName += ety.X_CNAME + ",";
            }
            SelNo = SelNo.TrimEnd(',');
            SelName = SelName.TrimEnd(',');
            this.DialogResult = DialogResult.OK;
        }

        private void tsbExit_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void gdSrc_DoubleClick(object sender, EventArgs e)
        {
            SrcToDst();
        }

        private void gdDst_DoubleClick(object sender, EventArgs e)
        {
            DstToSrc();
        }
    }
}
